import os
import shutil
import sys
from pathlib import Path

import pytest
from django import setup


result_id = sys.argv[-1]
sys.argv = sys.argv[:-2]

path = Path(__file__).parents[1]
sys.path.append(str(path))

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pytest_log.settings")
setup()

from case.models import RunResult
result: RunResult = RunResult.objects.get(id=result_id)  # 后续对数据库进行操作

result.status = result.RunStatus.Running
result.save()
# 启动框架，得到结果
ret_code = pytest.main()

if ret_code == pytest.ExitCode.OK:
    print("测试通过")
    result.is_pass = True
    result.save()
else:
    print("测试失败")
    result.status = result.RunStatus.Error
    result.save()

result.status = result.RunStatus.Reporting
result.save()

# 生成测试报告
ret_code = os.system("allure generate report/temp -o report/allure --clean")

if ret_code == pytest.ExitCode.OK:
    print("报告生成成功！")
    result.status = result.RunStatus.Done
    result.save()
else:
    print("报告生成失败！")
    result.status = result.RunStatus.Error
    result.save()

result: RunResult = RunResult.objects.get(id=result_id)
print(result_id, result.is_pass, result.status)






# import os
# import sys
# from pathlib import Path
# import pytest
# from django import setup
# from suite.models import RunResult
#
# result_id = sys.argv[-1]
# sys.argv = sys.argv[:-2]
#
# path = Path(__file__).parents[1]
# sys.path.append(str(path))
#
# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pytest_log.settings")
# setup()
#
# result: RunResult = RunResult.objects.get(id=result_id)
#
# result.status = result.RunStatus.Running
# result.save()
# ret_code = pytest.main()  # 启动框架，得到结果
#
# if ret_code == pytest.ExitCode.OK:
#     print("测试通过")
#     result.is_pass = True
#     result.save()
# else:
#     print("测试失败")
#
# result.status = result.RunStatus.Reporting
# result.save()
# ret_code = os.system("allure generate -o report temps --clean")  # 生成测试报告
#
# if ret_code == pytest.ExitCode.OK:
#     print("报告生成成功")
#     result.status = result.RunStatus.Done
#     result.save()
# else:
#     print("报告生成失败")
#     result.status = result.RunStatus.Error
#     result.save()
#
# result: RunResult = RunResult.objects.get(id=result_id)
#
